package tienda.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import tienda.modelo.Persona;
import tienda.negocio.GestionarCompra;
import tienda.negocio.GestionarPublicaciones;
/**
 * Servlet implementation class CompraServlet
 */
@WebServlet("/CompraServlet")
public class CompraServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public CompraServlet() {
    	super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
		HttpSession session = request.getSession();
		//recupero datos
		int idPublicacion = Integer.parseInt(request.getParameter("idPublicacion"));
		int cantidad = Integer.parseInt(request.getParameter("cantidad"));
		String comentario = request.getParameter("comentario");
		Persona usuario;
		usuario=(Persona)session.getAttribute("usuario");
		int idPersona =usuario.getIdPersona();
				
		
		
		RequestDispatcher rd = request.getRequestDispatcher("resultadocompra.jsp");
						
			try {
				//Registro compra
				Persona vendedor;
				GestionarPublicaciones negoPubli = new GestionarPublicaciones();
				vendedor=negoPubli.registraCompras(idPublicacion,cantidad,idPersona,comentario);					
				
				request.setAttribute("vendedor",  vendedor);						
				
			} catch (Exception e) {
				rd = request.getRequestDispatcher("error.jsp");
				request.setAttribute("error", e.getMessage());				
			}
				
		rd.forward(request, response);
				
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

}
